/*
 * TreeTransferHandler.java
 */
package com.vs.kindlecoll.UI;

import com.vs.kindlecoll.Beans.Book;
import javax.swing.*;
import java.awt.datatransfer.*;
import java.util.List;

public class ListTransferHandler extends TransferHandler {
            
    @Override
    public boolean canImport(TransferHandler.TransferSupport info) {
        // TODO:Work with array but not the first element
        if (!info.isDataFlavorSupported(new Book().getTransferDataFlavors()[0])) {
            return false;
        }
        return true;
   }

    @Override
    protected Transferable createTransferable(JComponent c) {
        return new Book(exportBooks(c)[0]);
    }
    
    @Override
    public int getSourceActions(JComponent c) {
        return TransferHandler.COPY_OR_MOVE;
    }
    
    @Override
    public boolean importData(TransferHandler.TransferSupport info) {
        if (!info.isDrop()) {
            return false;
        }
        //TODO: implement for Book from filesystem
        return true;
    }

    @Override
    protected void exportDone(JComponent c, Transferable data, int action) {
        
    }

    //Bundle up the selected items in the list
    //as a single string, for export.
    protected Book[] exportBooks(JComponent c) {
        JList list = (JList)c;
        List<Book> values = list.getSelectedValuesList();
        
        return values.toArray(new Book[values.size()]);
    }

}
